#!/usr/bin/python
import sys
import os
import pexpect

def expectandshow(child, expect):
        index=child.expect(expect)
        print child.before,child.after,
        return index

fp_intr = open('/root/vmimage/mpi_paas/code/ip','r')

Master_IP=fp_intr.readline()
length=len(Master_IP)
Master_IP=Master_IP[0:length-1]
print Master_IP
password="passw0rd"

while True:
	Salve_IP=fp_intr.readline()
	length=len(Salve_IP)
	if length == 0:
		break
	Salve_IP=Salve_IP[0:length-1]

	argv1="/dev/VolGroup00/LogVol00 /                       ext3    defaults        1 1"
	argv2="LABEL=/boot             /boot                   ext3    defaults        1 2"
	argv3="tmpfs                   /dev/shm                tmpfs   defaults        0 0"
	argv4="devpts                  /dev/pts                devpts  gid=5,mode=620  0 0"
	argv5="sysfs                   /sys                    sysfs   defaults        0 0"
	argv6="proc                    /proc                   proc    defaults        0 0"


	cmd = "ssh root@"+Salve_IP+" " + \
		"echo \'" + argv1 + "\' > /etc/fstab;" + \
		"echo \'" + argv2 + "\' >> /etc/fstab;" + \
		"echo \'" + argv3 + "\' >> /etc/fstab;" + \
		"echo \'" + argv4 + "\' >> /etc/fstab;" + \
		"echo \'" + argv5 + "\' >> /etc/fstab;" + \
		"echo \'" + argv6 + "\' >> /etc/fstab;" + \
		"echo \'"+ Master_IP+":/home /home nfs defaults 0 0\' >> /etc/fstab ;" + \
		"/sbin/chkconfig netfs off;" + \
		"/sbin/chkconfig netfs on;" + \
		"mount /home"

	print cmd
	scp_child=pexpect.spawn(cmd)
	index=expectandshow(scp_child, \
                                ['Are you sure you want to continue connecting', \
                                "root@"+Salve_IP+"'s password:", \
                                pexpect.TIMEOUT, \
                                pexpect.EOF])
	if index == 0:
        	scp_child.sendline('yes')
        	ret = expectandshow(scp_child, ["root@"+Salve_IP+"'s password:", pexpect.EOF])
        	if ret == 0:
                	scp_child.sendline(password)
			expectandshow(scp_child, [pexpect.EOF])
        	elif ret == 1:
                	pass
	elif index == 1:
        	scp_child.sendline(password)
		expectandshow(scp_child, [pexpect.EOF])
	elif index == 2:
        	print "timeout, ignore and continue."
	elif index == 3:
        	pass
fp_intr.close()
